package com.example.demo.Service;
/**
 * @作者:邹蕴果
 * @创建时间：2021/7/19
 * @修改时间：2021/7/20
 */
import com.example.demo.Calculate.CsvExportHandler;
import com.example.demo.EntityClass.Customer;
import com.example.demo.EntityClass.Points;
import com.example.demo.EntityClass.SKU;
import com.example.demo.Repository.PointsRepository;
import com.example.demo.Repository.CustomerRepository;
import com.example.demo.Repository.SKURepository;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class PointService {

    private PointsRepository pointsRepository;
    private CustomerRepository customerRepository;
    private SKURepository skuRepository;

    @Autowired
    public void setPointsRepository (PointsRepository pointsRepository) {
        this.pointsRepository = pointsRepository;
    }
    @Autowired
    public void setRecommendRepository (CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }
    @Autowired
    public void setSKURepository (SKURepository skuRepository) {
        this.skuRepository = skuRepository;
    }
    //获取评分表并存储为CSV文件
    public  List<Points> getPoints() {

        CsvExportHandler<Points> csvHandler=new CsvExportHandler<Points>();
        List<Points> points =pointsRepository.findAll();
        String property = System.getProperty("user.dir");
        String filePath =  "/CSV/User.csv";
        try {
            csvHandler.exportCsvFile(points,filePath);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return points;
    }
    //根据CSV文件进行推荐算法过滤设置推荐商品
    public void setPoints() throws IOException, TasteException {
        final  int NEIGHBORHOOD_NUM = 2;
        final  int RECOMMENDER_NUM = 3;
        //读取数据文件建立模型
        String file = "/CSV/User.csv";
        DataModel model = new FileDataModel(new File(file));
        UserSimilarity user = new EuclideanDistanceSimilarity(model);
        NearestNUserNeighborhood neighbor = new NearestNUserNeighborhood(NEIGHBORHOOD_NUM, user, model);
        //获得推荐列表
        Recommender r = new GenericUserBasedRecommender(model, neighbor, user);
        LongPrimitiveIterator iter = model.getUserIDs();

        while (iter.hasNext()) {
            int uid =(int) iter.nextLong();
           Customer CurrenCustomer= customerRepository.findById(uid);
           //获取单个用户推荐商品列表
            List<RecommendedItem> list = r.recommend(uid, RECOMMENDER_NUM);
            List<SKU>SKUList=new ArrayList<>();

            for (RecommendedItem ritem : list) {

                SKU CurrentSKU=skuRepository.findById((int)ritem.getItemID());
                SKUList.add(CurrentSKU);
            }

            CurrenCustomer.setRecommendList(SKUList);
            customerRepository.saveAndFlush(CurrenCustomer);

        }

    }
}
